<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      // fliter 循环过滤 有返回值
      var arr = [1, 2, 3, 4, 5]
      var resArr = arr.map((item) => item + 10)

      function myMap(fn, arr) {
        var lst = []
        for (let i = 0; i < arr.length; i++) {
          var res = fn(arr[i], i, arr)
          lst.push(res)
        }
        return lst
      }
      var arr1 = myMap(function (item) {
        return item + 10
      }, arr)
      console.log(arr1, ',,,,')

      resArr = arr.filter((item) => item > 3)
      console.log(resArr)

      function myFilter(fn, arr) {
        let new_arr = []
        for (let i = 0; i < arr.length; i++) {
          let res = fn(arr[i], i, arr)
          if (res) {
            new_arr.push(arr[i])
          }
        }
        return new_arr
      }

      let new_arr = myFilter(function (item) {
        return item > 4
      }, arr)
      var obj_Arr = [
        { name: '张三', age: 18, sex: '女' },
        { name: '张三1', age: 21, sex: '男' },
        { name: '张三2', age: 15, sex: '男' },
        { name: '张三3', age: 21, sex: '男' },
        { name: '张三4', age: 12, sex: '男' },
        { name: '张三5', age: 30, sex: '女' },
        { name: '张三', age: 18, sex: '女' },
        { name: '张三', age: 18, sex: '女' },
      ]
      //    console.log( obj_Arr.filter(item=>item.sex=='女'));
      //    console.log(obj_Arr.filter(item=>item.sex=='女').filter(item=>item.age>18));

      // var arr = [{name:"张三1",age:20,gender:"女"},{name:"张三2",age:21,gender:"男"},{name:"张三3",age:16,gender:"女"},{name:"张三4",age:25,gender:"女"},{name:"张三5",age:23,gender:"女"},{name:"张三6",age:21,gender:"女"}];
      //  console.log( arr.filter(item=>item.gender==='女').sort((a,b)=>-a.age+b.age));

      var arr = [1, 2, 3, 4, 5, 2, 2, 2, 3, 4, 5, 6, 8]
      var arr1 = arr.reduce((pre, cur) => {
        if (pre.indexOf(cur) === -1) {
          pre.push(cur)
        }
        return pre
      }, [])
      var obj = arr.reduce((pre, cur) => {
        if (pre[cur]) {
          pre[cur]++
        } else {
          pre[cur] = 1
        }
        return pre
      }, {})
      console.log(obj)

      function reDuce(fn, arr, pre) {
        let i = 0
        if (typeof pre === 'undefined') {
          pre = arr[0]
          i = 1
        }
        for (i; i < arr.length; i++) {
          var res = fn(pre, arr[i], i, arr)
          pre = res
        }
        return pre
      }
      var arr = [1, 2, 3, 4, 5, 5, 5, 5, 5]
      let result = reDuce(
        function (pre, cur) {
          return pre + cur
        },
        arr,
        0
      )
      var res = reDuce(
        (pre, cur) => {
          if (pre.indexOf(cur) === -1) {
            pre.push(cur)
          }
          return pre
        },
        arr,
        []
      )
      var arr = ['a', 'b', 'a', 'a', 'b', 'c']
    const resa =  reDuce(
        function (pre, item) {
            
          
            if (pre[item]) {
                 pre[item]++
            } else {
              pre[item] = 1
            }
          return pre
        },
        arr,
        {}
      )
      console.log(resa)
    </script>
  </body>
</html>
